package cn.scw.common.utils;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtils {
    /**
     * 手机号码正则
     */
    public static final String REGEX_PHONE = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
    /**
     * 固定号码正则，不带区号
     */
    public static final String REGEX_TELE_PHONE = "^[1-9]{1}[0-9]{5,8}$";
    /**
     * 固定号码正则，带区号
     */
    public static final String REGEX_TELE_PHONE_AREA = "^[0][1-9]{2,3}-[0-9]{5,10}$";
    /**
     * ip地址正则
     */
    public static final String REGEX_IP = "(?<=(\\b|\\D))(((\\d{1,2})|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))\\.){3}((\\d{1,2})|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))(?=(\\b|\\D))";
    /**
     * email地址
     */
    public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";


    /**
     * 验证手机号码
     *
     * @param phone 手机号码
     */
    public static boolean isPhone(String phone) {
        if (phone.length() != 11) {
            return false;
        }
        Pattern p = Pattern.compile(REGEX_PHONE);
        Matcher m = p.matcher(phone);
        return m.matches();
    }

    /**
     * 验证固定号码
     *
     * @param phone 手机号码
     */
    public static boolean isTelePhone(String phone) {
        Matcher m;
        Pattern p1 = Pattern.compile(REGEX_TELE_PHONE_AREA); // 验证带区号的
        Pattern p2 = Pattern.compile(REGEX_TELE_PHONE);
        if (phone.length() > 9) {
            m = p1.matcher(phone);
            return m.matches();
        } else {
            m = p2.matcher(phone);
            return m.matches();
        }
    }

    /**
     * 验证ipV4
     *
     * @param ip ip
     */
    public static boolean isIp(String ip) {
        Pattern p = Pattern.compile(REGEX_IP);
        Matcher m = p.matcher(ip);
        return m.matches();
    }

    /**
     * 验证email地址
     *
     * @param email email地址
     */
    public static boolean isEmail(String email) {
        Pattern p = Pattern.compile(REGEX_EMAIL);
        Matcher m = p.matcher(email);
        return m.matches();
    }

}
